package Reminder.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import Reminder.dao.UserDao;
import Reminder.domain.Message;
import Reminder.domain.Utils;

import java.io.*;
import java.text.ParseException;

public class UserDaoImpl implements UserDao {
    @Override
    public Message setUserData(JSONObject useInfo) throws IOException {
        FileOutputStream fos = new FileOutputStream("user.txt");
        fos.write(useInfo.toString().getBytes());
        fos.close();
        return new Message("setUserData","修改成功","success");
    }

    @Override
    public Message getUserData(int phone) throws IOException, ParseException {
        FileInputStream fis = new FileInputStream("user.txt");
        int len = 0;
        byte bytes[] = new byte[200];
        fis.read(bytes);

//        User res = new User(JSONObject.parseObject(new String(bytes)));
        System.out.println(JSONObject.parseObject(new String(bytes)).toString());
        fis.close();
        return new Message("getUserData",JSONObject.parseObject(new String(bytes)).toString(),"success");
    }

    @Override
    public Message exist(String phone) throws IOException {
        Message msg = getUserList();
        if(msg.toJSON().get("status") == "success") {
            JSONArray userList = JSONArray.parseArray(msg.toJSON().getString("data"));
            for(int i = 0;i < userList.size();i++){
                System.out.println(((JSONObject)userList.get(i)).toString());
                if(((JSONObject)userList.get(i)).getString("phone").equals(phone)){
                    return new Message("exist",i + "","success");
                }
            }
            return new Message("exist","false","success");
        }
        else{
            return  msg;
        }
    }

    @Override
    public Message register(JSONObject userInfo) throws ParseException, IOException {
        Message msg = exist(userInfo.getString("phone"));
        if(msg.toJSON().getString("status").equals("success") && msg.toJSON().getString("data").equals("false")){
            msg = getUserList();
            JSONArray userList = JSONArray.parseArray(msg.toJSON().getString("data"));
            if(msg.toJSON().getString("status").equals("success")){
                msg = generateUserId();
                if(msg.toJSON().getString("status").equals("success")) {
                    userInfo.put("userId",msg.toJSON().getInteger("data") + 1000);
                    userList.add(userInfo);
                    msg = Utils.setTXT("data/userList.txt",userList.toString());
                    if(msg.toJSON().getString("status").equals("success")){

                        return  new Message("register","","success");
                    }
                }
            }
        }
        return msg;
    }

    @Override
    public Message login(JSONObject userInfo) throws IOException {
        Message msg = exist(userInfo.getString("phone"));
        if(msg.toJSON().getString("status").equals("success") && !msg.toJSON().getString("data").equals("false")){
            int index = msg.toJSON().getInteger("data");
            msg = getUserList();

            if(msg.toJSON().getString("status").equals("success")){
                JSONArray userList = JSONArray.parseArray(msg.toJSON().getString("data").toString());
                JSONObject userData = ((JSONObject)userList.get(index));
                if (userInfo.getString("password").equals(userData.getString("password"))){
                    return new Message("login",userData.toString(), "success");
                }
                else{
                    return new Message("login","登录失败，密码错误","error");
                }
            }
        }
        else{
            return new Message("login","登录失败,该手机号并没有注册","error");
        }
        return msg;
    }

    @Override
    public Message getUserList() throws IOException {
        File file = new File("data/userList.txt");
        if(!file.exists()){
            if(file.createNewFile()){
                JSONArray userList = new JSONArray();
                JSONObject origin = JSONObject.parseObject("{\"gender\":false,\"password\":\"10000000000\",\"phone\":\"11111111111\",\"userId\":1008,\"age\":0}");
                userList.add(origin);
                Utils.setTXT("data/userList.txt",userList.toString());
                return new Message("getUserList","暂无数据,已经完成数据初始化","error");
            }
            else{
                return new Message("getUserList","暂无数据，数据初始化失败","error");
            }
        }
        else{
            Message msg = Utils.loadTXT("data/userList.txt");
            if(msg.toJSON().getString("status") == "suucess"){
                JSONArray data = JSONArray.parseArray(msg.toJSON().getString("data"));
                return new Message("getUserList",data.toString(),"success");
            }
            else{
                return msg;
            }
        }
    }

    @Override
    public Message generateUserId() throws IOException {
        Message msg = getUserList();
        if(msg.toJSON().get("status") == "success") {
            JSONArray userList = JSONArray.parseArray(msg.toJSON().getString("data"));
            return new Message("generateUserId",(userList.size() + 1) + "","success");
        }
        else{
            return  msg;
        }
    }

    public static void main(String[] args) throws IOException {
        UserDao userDao = new UserDaoImpl();
        System.out.println(userDao.exist("11111111111"));
    }
}
